const app = getApp();

const QQMapWX = require('../libs/qqmap-wx-jssdk.min.js');


export const locationMixin = {
  
  data: {
    address: "",
  },

  onLoad: function (options) {
    console.log("appInfo", app);

    wx.getLocation({
      type: 'gcj02',
      success: res=> {

        var latitude = res.latitude;
        var longitude = res.longitude;
        this.getLocation(latitude,longitude);
        },
    });
},
getLocation:function(latitude,longitude,showLoading=false){
  console.log(latitude,longitude);
  let qqmapsdk = new QQMapWX({
    key: "LRRBZ-4NFC6-XPBSB-E3F6V-PU565-D7B5X"// 必填
  });
  // var _this = this;
  // console.log(this);
  if(showLoading){
    wx.showLoading({
      title: '定位中...',
    })
  }


  qqmapsdk.reverseGeocoder({
    success: function(res) {//成功后的回调
      
      var res = res.result;
      // console.log(resgit);
    },
    fail: function(error) {
      console.error(error);
    },
    complete: (res)=> {

      if(showLoading){

        wx.hideLoading({
          success: (res) => {},
        })

        wx.showToast({
          mask: true,
          title: "位置刷新成功",
          icon: "none",
          duration: 2000,
      });

      }

      var res = res.result;
       console.log(res);
      console.log(res.address);
      this.setData({
          latitude: latitude,
          longitude: longitude,
          address:res.address,
          markers:[  {
            id: 0,
            latitude: latitude,
            longitude: longitude,
            callout:{
              content: res.address,
              padding:10,
              display:'ALWAYS',
              textAlign:'center',
            }
          },
        ]
        })
    }
  })

},
};